
//
// !!!修改文件后调用 npm run buildJs 生成 ui-utils-all.js
//

"use strict";

import { uiUtils } from "./ui-utils-core";
import { globalUtils } from '../global-utils/global-utils-all';

/* SOURCE-CODE-START */

/**
 * @namespace uiUtils-view
 */

var _view = {};
uiUtils.view = _view;
var VIEW_SCOPE_LOCK_COUNT = 'uiUtils.lockCount';
var VIEW_SCOPE_FORM_DATA = 'uiUtils.formData';

/**
 * @memberof uiUtils-view
 * @description 锁定视图在离开时需要确认
 * @param {Object} scope 
 */
_view.lockViewConfirmLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  var lockCount = scope[VIEW_SCOPE_LOCK_COUNT];

  if (globalUtils.isNullOrUndefined(lockCount)) {
    lockCount = 1;
  } else {
    if (lockCount >= 1) {
      lockCount = lockCount + 1;
    } else {
      lockCount = 1;
    }
  }

  if (lockCount === 1) {
    scope.onViewClosing = function (view, nextFn) {
      uiUtils.dialog.showConfirmBox('${t("ui.system.common.msg.promptWillLeaveViewWhenLeave")}', nextFn);
    };
  }

  scope[VIEW_SCOPE_LOCK_COUNT] = lockCount;
};

/**
 * @memberof uiUtils-view
 * @description 解锁视图在离开时不需要确认
 * @param {Object} scope 
 */
_view.unlockViewAllowLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  var lockCount = scope[VIEW_SCOPE_LOCK_COUNT];

  if (globalUtils.isNullOrUndefined(lockCount)) {
    lockCount = 0;
  } else {
    if (lockCount >= 1) {
      lockCount = lockCount - 1;
    } else {
      lockCount = 0;
    }
  }

  if (lockCount === 0) {
    scope.onViewClosing = undefined;
  }

  scope[VIEW_SCOPE_LOCK_COUNT] = lockCount;
};

/**
 * @memberof uiUtils-view
 * @description 离开视图时检查表单数据是否未保存
 * @param {Object} scope 
 * @param {function} dataFn
 */
_view.checkFormDataChangeWhenLeave = function (scope, dataFn) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(dataFn)) {
    throw new Error('argument#1 "dataFn" is null/undefined');
  }

  // 记录当前表单数据到上下文中
  _view.setCurrentFormDataToContext(scope, dataFn);

  scope.onViewClosing = function (view, nextFn) {
    var newFormData = dataFn();
    var oldFormData = scope[VIEW_SCOPE_FORM_DATA];

    if (globalUtils.isFormDataEqual(oldFormData, newFormData)) {
      nextFn();
    } else {
      uiUtils.dialog.showConfirmBox('${t("ui.system.common.msg.promptDataNotSavedWhenLeave")}', nextFn);
    }
  };
};

/**
 * @memberof uiUtils-view
 * @description 离开视图时不检查表单数据是否未保存
 * @param {Object} scope 
 */
_view.notCheckFormDataChangeWhenLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  scope.onViewClosing = undefined;
};


/**
 * @memberof uiUtils-view
 * @description 记录当前表单数据到上下文中
 * @param {Object} scope 
 * @param {(function|*)} data
 */
_view.setCurrentFormDataToContext = function (scope, data) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(data)) {
    throw new Error('argument#1 "data" is null/undefined');
  }

  scope[VIEW_SCOPE_FORM_DATA] = globalUtils.isFunction(data)
    ? data() : data;
};

/* SOURCE-CODE-END */

export { uiUtils, _view };
